/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.oa.constants.FunctionEnum;
import com.legendshop.oa.constants.ProjectStatusEnum;
import com.legendshop.oa.dao.ProjectDao;
import com.legendshop.oa.dao.ProjectMemberDao;
import com.legendshop.oa.dto.ProjectQueryDto;
import com.legendshop.oa.dto.ProjectReport;
import com.legendshop.oa.dto.ProjectReportQueryDto;
import com.legendshop.oa.dto.Select2Dto;
import com.legendshop.oa.exception.BusinessException;
import com.legendshop.oa.model.KeyValueEntity;
import com.legendshop.oa.model.Project;
import com.legendshop.oa.security.UserDetail;
import com.legendshop.oa.service.ProjectService;
import com.legendshop.util.AppUtils;

import java.util.Date;
import java.util.List;

/**
 * The Class ProjectServiceImpl. 服务实现类
 */
public class ProjectServiceImpl implements ProjectService {

	/**
	 *
	 * 引用的Dao接口
	 */
	private ProjectDao projectDao;
	
	/**
	 *
	 * 项目组成员Dao
	 */
	private ProjectMemberDao projectMemberDao;

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	/**
	 * 根据商城获取列表
	 */
	public List<Project> getProject(String userName) {
		return projectDao.getProject(userName);
	}
	
	@Override
	public List<ProjectReport> getProjectReport(ProjectReportQueryDto projectReportQueryDto) {
		return projectDao.getProjectReport(projectReportQueryDto);
	}

	/**
	 * 根据project_Id获取
	 */
	public Project getProject(UserDetail userDetail, Long id) {
		Project project = projectDao.getProject(id);
		if(project == null) {
			return project;
		}
		if(isProjectAccessable(project, userDetail)) {
			return project;
		}
		throw new BusinessException(userDetail.getUsername() + " can not access project with id " + id);
	}

	/**
	 * 删除
	 */
	public void deleteProject(UserDetail userDetail, Project project) {
		if(!isProjectDeleteable(project, userDetail)) {
			throw new BusinessException(userDetail.getUsername() + " can not delete project with id " + project.getId());
		}
		projectDao.deleteProject(project);
	}

	/**
	 * 保存
	 */
	public Long saveProject(Project project, UserDetail ud) {
		if(!isProjectWritable(project, ud)) {
			throw new BusinessException(ud.getUsername() + " can not save project " + project.getId());
		}
		
		if (!AppUtils.isBlank(project.getId())) {
			Project origin = projectDao.getProject(project.getId());
			origin.setName(project.getName());
			origin.setAliasName(project.getAliasName());
			origin.setType(project.getType());
			origin.setStartTime(project.getStartTime());
			origin.setEndTime(project.getEndTime());
			origin.setEstimateHours(project.getEstimateHours());
			origin.setPrincipal(project.getPrincipal());
			origin.setTester(project.getTester());
			origin.setMaxHoursPerweek(project.getMaxHoursPerweek());
			if(project.getStatus() != null) {
				origin.setStatus(project.getStatus());
			}
			origin.setRemark(project.getRemark());
			
			updateProject(origin);
			return project.getId();
		}
		
    	Date date = new Date();
    	project.setCreateTime(date);
    	project.setCreater(ud.getUserId().toString());
    	if(project.getStatus() == null) {
    		project.setStatus(ProjectStatusEnum.UNSTART.value());//刚开始为未开始状态
    	}
		return projectDao.saveProject(project);
	}

	/**
	 * 更新
	 */
	public void updateProject(Project project) {
		projectDao.updateProject(project);
	}

	@Override
	public PageSupport<Project> getProject(ProjectQueryDto projectQueryDto) {
		return projectDao.getProject(projectQueryDto);
	}

	@Override
	public Project getProjectWithModule(Long projectId) {
		return projectDao.getProjectWithModule(projectId);
	}

	/**
	 * 查询用户拥有的项目
	 */
	@Override
	public List<KeyValueEntity> loadProjectsEntity(UserDetail userDetail) {
		if(userDetail.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA)) {
			return projectDao.loadProjectsEntity();
		}else {
			return projectDao.loadProjectsEntity(userDetail);
		}
		
	}

	@Override
	public List<KeyValueEntity> loadModulesEntity(Integer projectId) {
		return projectDao.loadModulesEntity(projectId);
	}

	@Override
	public List<KeyValueEntity> loadPlansEntity(Integer projectId) {
		return projectDao.loadPlansEntity(projectId);
	}


	public void setProjectMemberDao(ProjectMemberDao projectMemberDao) {
		this.projectMemberDao = projectMemberDao;
	}

	/**
	 * 检查项目是否可以访问
	 */
	@Override
	public boolean isProjectAccessable(Project project, UserDetail userDetail) {
		//0:普通项目,1:公共项目, 公共项目是所有人都可以访问
				if(project.getType() == 1) {
					return true;
				}		
				//超级管理员可以查看所有的数据
				if(userDetail.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA)) {
					return true;
				}
				
				//检查权限,如果项目经理或者测试经理才可以访问
				if(userDetail.getUsername().equals(project.getPrincipal()) || userDetail.getUsername().equals(project.getTester())) {
					return true;
				}
				boolean result = projectMemberDao.isMemberInProject(userDetail.getUsername(), project.getId());
				//boolean result = true; //所以项目都开放
				return result;
		
	}
	
	/**
	 * 检查项目是否可以访问
	 */
	@Override
	public boolean isProjectWritable(Project project, UserDetail userDetail) {
				//超级管理员可以查看所有的数据
				//必须要有新建项目的权限
				if(userDetail.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA)) {
					return true;
				}
				
				//检查权限,如果项目经理才可以访问
				if(userDetail.hasFunction(FunctionEnum.FUNCTION_NEW_PROJECT)) {
					return true;
				}
				return false;
		
	}
	
	/**
	 * 检查项目是否可以访问
	 */
	@Override
	public boolean isProjectDeleteable(Project project, UserDetail userDetail) {
				//超级管理员可以查看所有的数据
				//必须要有新建项目的权限
				if(userDetail.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA)) {
					return true;
				}
				
				//检查权限,如果项目经理或者测试经理才可以访问
				if(userDetail.hasFunction(FunctionEnum.FUNCTION_DELETE_PROJECT) && userDetail.getUsername().equals(project.getPrincipal())) {
					return true;
				}
				return false;
		
	}

	@Override
	public boolean updateProjectWorkingTime(Long projectId) {
		return projectDao.updateProjectWorkingTime(projectId);
		
	}

	@Override
	public List<KeyValueEntity> loadMyProjectsEntity(UserDetail userDetail) {
		return projectDao.loadProjectsEntity(userDetail);
	}

	@Override
	public JSONObject loadMyProjectEntities(String username, String q, Integer pageSize, Integer currPage) {
		List<Select2Dto> list = projectDao.queryProjectList(username, q, pageSize, currPage);
		int total = projectDao.projectCount(username, q);
		JSONObject result = new JSONObject();
		result.put("results", list);
		result.put("total", total);
		return result;
	}



}
